package com.h3c.web.support.cas.servlet;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URISyntaxException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.h3c.dal.common.TableConstant;
import com.h3c.dal.dao.CasssoOrganizationUnitDAO;
import com.h3c.dal.dao.CasssoSystemDAO;
import com.h3c.dal.entity.CasssoOrganizationUnitDO;
import com.h3c.dal.entity.CasssoSystemDO;
import com.h3c.dal.entity.query.CasssoOrganizationUnitQuery;
import com.h3c.dal.entity.query.CasssoSystemQuery;
import com.h3c.web.common.utils.SpringContextUtil;

/**
 * @ClassName: MultiOrgServlet
 * @Description: 多组织页面的展现
 * @author: d14287
 * @date: 2017年6月3日 上午10:10:52
 */
public class MultiOrgServlet extends HttpServlet {
	private static final long serialVersionUID = -2588701699498105828L;
	private static final Logger logger = LoggerFactory.getLogger(MultiOrgServlet.class);
	private CasssoSystemDAO casssoSystemDAO;
	private CasssoOrganizationUnitDAO casssoOrganizationUnitDAO;

	@Override
	public void init() throws ServletException {
		super.init();
		this.casssoSystemDAO = SpringContextUtil.getBean(CasssoSystemDAO.class);
		this.casssoOrganizationUnitDAO = SpringContextUtil.getBean(CasssoOrganizationUnitDAO.class);
	}
	@SuppressWarnings("unchecked")
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		try {
			String login_url = req.getParameter("login_url");
			CasssoSystemQuery sysQuery = new CasssoSystemQuery();
			sysQuery.createCriteria().andStatusEqualTo(TableConstant.COMMON_STATUS_NORMAL);
			List<CasssoSystemDO> sysList = casssoSystemDAO.selectByExample(sysQuery);
			CasssoSystemDO dest = null;
			for (CasssoSystemDO casssoSystemDO : sysList) {
				if (login_url.contains(casssoSystemDO.getUrl())) {
					dest = casssoSystemDO;
					break;
				}
			}
			// 找到组织
			if (dest != null) {
				CasssoOrganizationUnitQuery orgQuery = new CasssoOrganizationUnitQuery();
				orgQuery.createCriteria().andOuIdEqualTo(dest.getOuId())
						.andStatusEqualTo(TableConstant.COMMON_STATUS_NORMAL);
				List<CasssoOrganizationUnitDO> orgList = casssoOrganizationUnitDAO.selectByExample(orgQuery);
				byte[] imgbyte = orgList.get(0).getBackgroundPic();
				resp.setHeader("content-type", "image/jpeg");
				resp.getOutputStream().write(imgbyte);
			} else {
				readDefaultImg(resp);
			}
		} catch (Exception e) {
			logger.error("happend error");
		}
	}
	public void readDefaultImg(HttpServletResponse resp) throws URISyntaxException, IOException {
		ClassLoader classLoader = getClass().getClassLoader();
		InputStream input = classLoader.getResourceAsStream("login.jpg");
		resp.setHeader("content-type", "image/jpeg");
		resp.getOutputStream().write(input2byte(input));
	}
	public static final byte[] input2byte(InputStream inStream) throws IOException {
		ByteArrayOutputStream swapStream = new ByteArrayOutputStream();
		byte[] buff = new byte[100];
		int rc = 0;
		while ((rc = inStream.read(buff, 0, 100)) > 0) {
			swapStream.write(buff, 0, rc);
		}
		byte[] in2b = swapStream.toByteArray();
		return in2b;
	}
}
